﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication4 {
    public static class Helpers {
        /// <summary>
        /// Pivot the original data table, resulting a new pivot'ed data table
        /// </summary>
        /// <param name="originalTable">The original datatable to be pivot'ed</param>
        /// <param name="tableName">Table name of the result data table</param>
        /// <param name="captionColumn">Table name of the result data table</param>
        /// <param name="pivotColumn">Table name of the result data table</param>
        /// <returns>The pivot'ed data table</returns>
        public static DataTable Pivot(this DataTable originalTable, string tableName, string pivotColumn) {

            DataTable newTable = new DataTable(tableName);
            newTable.Columns.Add(new DataColumn("Type", typeof(string)));
            var distinctRow = originalTable.Select().DistinctBy(x => x[pivotColumn]);
            foreach (DataRow row in distinctRow) {
                DateTime date = (DateTime)row[pivotColumn];
                newTable.Columns.Add(new DataColumn(date.ToString("yyyy-MM-dd"), typeof(decimal)));
            }

            for (int i = 1; i < originalTable.Columns.Count; i++) {
                DataRow dr = newTable.NewRow();
                dr[0] = originalTable.Columns[i].ColumnName;
                newTable.Rows.Add(dr);
            }

            foreach (var r in originalTable.Select()) {
                for (int i = 1; i < originalTable.Columns.Count; i++) {
                    string colName = r.Table.Columns[i].ColumnName;
                    var newRow = newTable.Select().Where(x => x[0].ToString() == r.Table.Columns[i].ColumnName).First();
                    DateTime date = (DateTime)r[pivotColumn];
                    newRow[date.ToString("yyyy-MM-dd")] = r[colName];
                }
            }

            return newTable;
        }

        public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) {
            HashSet<TKey> seenKeys = new HashSet<TKey>();
            foreach (TSource element in source) {
                if (seenKeys.Add(keySelector(element))) {
                    yield return element;
                }
            }
        }

        /// <summary>
        /// 分页用的扩展方法，返回分页后的 Data Table
        /// </summary>
        /// <param name="fullTable"></param>
        /// <param name="PagedTableName"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static DataTable Paging(this DataTable fullTable, string PagedTableName, int pageSize, int pageIndex) {
            
            var datetimeSet = fullTable.Select().DistinctBy(x => x["Date"]).OrderBy(x=>x["Date"]);
            DataTable dt = new DataTable(PagedTableName);
            foreach (DataColumn c in fullTable.Columns) {
                dt.Columns.Add(c.ColumnName, c.DataType);
            } 
            var dtArray = datetimeSet.ToArray();
            for (int i = pageIndex * pageSize; i < (pageIndex + 1) * pageSize; i++) {
                if ((i + 1) >= dtArray.Count()) {
                    break;
                }
                DataRow row = dt.NewRow();
                for (int j = 0; j < fullTable.Columns.Count; j++) {
                    row[j] = dtArray[i][j];
                }
                dt.Rows.Add(row);
            }
            return dt;
        }
    }
}
